
package com.hlkj.pay.app.merchant.ext.impl.alipayZft;
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;

import com.alipay.api.AlipayClient;
import com.alipay.api.domain.*;
import com.alipay.api.request.AlipayTradeFastpayRefundQueryRequest;
import com.alipay.api.request.AlipayTradeOrderSettleQueryRequest;
import com.alipay.api.request.AlipayTradeOrderSettleRequest;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.alipay.api.response.AlipayTradeFastpayRefundQueryResponse;
import com.alipay.api.response.AlipayTradeOrderSettleQueryResponse;
import com.alipay.api.response.AlipayTradeOrderSettleResponse;
import com.alipay.api.response.AlipayTradeRefundResponse;
import com.hlkj.framework.common.pojo.CommonResult;
import com.hlkj.framework.common.util.json.JsonUtils;
import com.hlkj.framework.extension.annotation.Extension;
import com.hlkj.pay.app.merchant.ext.ChannelMerchantProfitOrderExtPt;
import com.hlkj.pay.app.merchant.ext.MerchantExtConstant;
import com.hlkj.pay.app.merchant.ext.dto.profit.ChannelProfitOrderContext;
import com.hlkj.pay.app.merchant.ext.dto.profit.ChannelProfitOrderQueryContext;
import com.hlkj.pay.app.merchant.ext.impl.alipay.dto.AlipayConfig;
import com.hlkj.pay.app.merchant.ext.impl.alipay.handler.AliPayCommonParamsHandle;
import com.hlkj.pay.app.merchant.ext.impl.alipayZft.dto.AliPayZftChannelParams;
import com.hlkj.pay.app.merchant.ext.impl.alipayZft.dto.AlipayZftDivideNotifyBizContentDto;
import com.hlkj.pay.common.MerchantResultCode;
import com.hlkj.pay.dto.order.PayOrderFundDto;
import com.hlkj.pay.dto.order.RefundOrderFundDto;
import com.hlkj.pay.enums.PayOrderEnums;
import com.hlkj.pay.infrastructure.model.merchant.MerchantAppChannelDO;
import com.hlkj.pay.infrastructure.model.order.OrderFundDO;
import com.hlkj.pay.infrastructure.model.order.OrderFundDetailDO;
import com.hlkj.pay.infrastructure.model.order.PayOrderDO;
import com.hlkj.pay.task.CommonTasks;
import com.hlkj.pay.util.DateUtils;
import com.ijpay.alipay.AliPayApi;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @author HlpayTeam
 * @date 2025/01/06 17:47
 */
@Slf4j
@Repository
@RequiredArgsConstructor
@Extension(bizId = MerchantExtConstant.CHANNEL_ALI_PAY_ZFT)
public class AliPayZftMerchantProfitOrderExtPt implements ChannelMerchantProfitOrderExtPt {

    private final AliPayCommonParamsHandle aliPayCommonParamsHandle;

    private final CommonTasks commonTasks;

    @Value("${hlkj.domain}")
    private String domain;

    @Override
    public void submitOrderProfit(ChannelProfitOrderContext channelProfitOrderContext) {
        // 判断商户类型
        MerchantAppChannelDO merchantAppChannelDO = channelProfitOrderContext.getMerchantAppChannelDO();
        // 是否普通商户
        boolean common = aliPayCommonParamsHandle.commonMer(merchantAppChannelDO);
        // 支付宝直付通配置信息
        CommonResult<AlipayConfig> alipayConfigCommonResult = null;
        // 普通商户
        if(common){
            channelProfitOrderContext.setRemark("支付宝直付通不支持普通商户");
            channelProfitOrderContext.setProcessStatus(false);
            return;
        }else{
            alipayConfigCommonResult = aliPayCommonParamsHandle.alipayConfig(channelProfitOrderContext.getProviderChannelConfig().getParamsConfig());
        }
        if(!alipayConfigCommonResult.isSuccess()){
            channelProfitOrderContext.setRemark(alipayConfigCommonResult.getMsg());
            channelProfitOrderContext.setProcessStatus(false);
            return;
        }
        PayOrderDO payOrderDO = channelProfitOrderContext.getPayOrderDO();
        try {
            // 获取AlipayClient
            AlipayClient client = aliPayCommonParamsHandle.initClient(alipayConfigCommonResult.getData());
            // 分账接收方列表
            List<PayOrderFundDto> payOrderFundList = channelProfitOrderContext.getPayOrderFundDtoList();
            // 批次号
            String batchNo = payOrderFundList.get(0).getBatchNo();
            // 请求实体
            AlipayTradeOrderSettleModel model = new AlipayTradeOrderSettleModel();
            model.setOutRequestNo(batchNo);
            model.setTradeNo(payOrderDO.getChannelOrderNo());
            // 分账明细信息
            List<OpenApiRoyaltyDetailInfoPojo> receivers = new ArrayList<>();
            for(PayOrderFundDto fundDto:payOrderFundList){
                OpenApiRoyaltyDetailInfoPojo royaltyDetailInfoPojo = new OpenApiRoyaltyDetailInfoPojo();
                royaltyDetailInfoPojo.setRoyaltyType("transfer");
                royaltyDetailInfoPojo.setTransInType("loginName");
                royaltyDetailInfoPojo.setTransIn(fundDto.getAccountNo());
                royaltyDetailInfoPojo.setAmount(fundDto.getAmount());
                royaltyDetailInfoPojo.setDesc(fundDto.getDescription());
                receivers.add(royaltyDetailInfoPojo);
            }

            // 分账剩余资金解冻
            SettleExtendParams settleExtendParams = new SettleExtendParams();
            settleExtendParams.setRoyaltyFinish("true");
            model.setExtendParams(settleExtendParams);

            model.setRoyaltyParameters(receivers);
            model.setRoyaltyMode("async");

            // 构造请求参数以调用接口
            AlipayTradeOrderSettleRequest request = new AlipayTradeOrderSettleRequest();
            request.setBizModel(model);

            log.info("支付宝直付通提交分账 请求参数:{}", JSONUtil.toJsonStr(model));
            AlipayTradeOrderSettleResponse response = client.execute(request);
            log.info("支付宝直付通提交分账 返回参数:{}", JSONUtil.toJsonStr(response));

            if (response.isSuccess()) {
                for(PayOrderFundDto payOrderFundDO:payOrderFundList){
                    PayOrderEnums.PAY_ORDER_FUND_STATUS subPayOrderFundStatus = PayOrderEnums.PAY_ORDER_FUND_STATUS.FUNDING;
                    payOrderFundDO.setFundStatus(subPayOrderFundStatus);
                }
            }else{
                channelProfitOrderContext.setProcessStatus(Boolean.FALSE);
                channelProfitOrderContext.setRemark(response.getSubMsg());
            }
        }
        catch (Exception e) {
            log.error("支付宝直付通提交订单分账请求失败 message:{}", e);
            channelProfitOrderContext.setRemark("支付宝直付通提交订单分账请求失败,失败信息:" + e.getMessage());
            channelProfitOrderContext.setProcessStatus(Boolean.FALSE);
        }
    }

    @Override
    public void queryOrderProfit(ChannelProfitOrderQueryContext channelProfitOrderContext) {
        // 判断商户类型
        MerchantAppChannelDO merchantAppChannelDO = channelProfitOrderContext.getMerchantAppChannelDO();
        // 是否普通商户
        boolean common = aliPayCommonParamsHandle.commonMer(merchantAppChannelDO);
        // 支付宝直付通配置信息
        CommonResult<AlipayConfig> alipayConfigCommonResult = null;
        // 普通商户
        if(common){
            channelProfitOrderContext.setRemark("支付宝直付通不支持普通商户");
            channelProfitOrderContext.setProcessStatus(false);
            return;
        }else{
            alipayConfigCommonResult = aliPayCommonParamsHandle.alipayConfig(channelProfitOrderContext.getProviderChannelConfig().getParamsConfig());
        }
        if(!alipayConfigCommonResult.isSuccess()){
            channelProfitOrderContext.setRemark(alipayConfigCommonResult.getMsg());
            channelProfitOrderContext.setProcessStatus(false);
            return;
        }
        PayOrderDO payOrderDO = channelProfitOrderContext.getPayOrderDO();
        try {
            // 获取AlipayClient
            AlipayClient client = aliPayCommonParamsHandle.initClient(alipayConfigCommonResult.getData());

            List<OrderFundDetailDO> payOrderFundList = channelProfitOrderContext.getPayOrderFunds();
            // 批次号
            String batchNo = payOrderFundList.get(0).getBatchNo();

            AlipayTradeOrderSettleQueryModel model = new AlipayTradeOrderSettleQueryModel();
            model.setOutRequestNo(batchNo);
            model.setTradeNo(payOrderDO.getChannelOrderNo());

            // 构造请求参数以调用接口
            AlipayTradeOrderSettleQueryRequest request = new AlipayTradeOrderSettleQueryRequest();
            request.setBizModel(model);

            log.info("支付宝直付通提交分账查询 请求参数:{}", JSONUtil.toJsonStr(model));
            AlipayTradeOrderSettleQueryResponse response = client.execute(request);
            log.info("支付宝直付通提交分账查询 返回参数:{}", JSONUtil.toJsonStr(response));

            if (response.isSuccess()) {
                for(OrderFundDetailDO fundDetailDO:payOrderFundList){
                    Optional<RoyaltyDetail> aliProfitReceiverModelOptional = response.getRoyaltyDetailList().stream().filter(receiver -> receiver.getTransIn().equals(fundDetailDO.getAccountNo())).findFirst();
                    if(aliProfitReceiverModelOptional.isPresent()){
                        RoyaltyDetail royaltyDetail = aliProfitReceiverModelOptional.get();
                        if(!royaltyDetail.getOperationType().equals("transfer")){
                            log.info("支付宝直付通提交分账查询 分账操作类型非分账：{}",JSONUtil.toJsonStr(royaltyDetail));
                            continue;
                        }
                        PayOrderEnums.PAY_ORDER_FUND_STATUS subPayOrderFundStatus = null;
                        String subFailMsg = null;
                        switch (royaltyDetail.getState()){
                            case "PROCESSING":
                                subPayOrderFundStatus = PayOrderEnums.PAY_ORDER_FUND_STATUS.FUNDING;
                                break;
                            case "SUCCESS":
                                subPayOrderFundStatus = PayOrderEnums.PAY_ORDER_FUND_STATUS.SUCCESS;
                                break;
                            case "FAIL":
                                subPayOrderFundStatus = PayOrderEnums.PAY_ORDER_FUND_STATUS.FAIL;
                                subFailMsg = royaltyDetail.getErrorDesc();
                                break;
                        }
                        fundDetailDO.setFundStatus(subPayOrderFundStatus.getCode());
                        fundDetailDO.setType(royaltyDetail.getTransInType());
                        fundDetailDO.setDetailId(royaltyDetail.getDetailId());
                        fundDetailDO.setSuccessTime(DateUtils.shanghaiDate(DateUtil.format(royaltyDetail.getExecuteDt(), DatePattern.NORM_DATETIME_PATTERN),  DatePattern.NORM_DATETIME_PATTERN));
                        if(StrUtil.isNotEmpty(subFailMsg)){
                            fundDetailDO.setFailReason(subFailMsg);
                        }
                    }
                }
            }else{
                channelProfitOrderContext.setProcessStatus(Boolean.FALSE);
                channelProfitOrderContext.setRemark(response.getSubMsg());
            }
        }
        catch (Exception e) {
            log.error("支付宝直付通提交分账查询请求失败 message:{}", e);
            channelProfitOrderContext.setRemark("支付宝直付通提交分账查询请求失败,失败信息:" + e.getMessage());
            channelProfitOrderContext.setProcessStatus(Boolean.FALSE);
        }
    }

    @Override
    public void submitRefundOrderProfit(ChannelProfitOrderContext channelProfitOrderContext) {
        log.info("支付宝直付通分账退款 请求参数:{}", JsonUtils.toJsonString(channelProfitOrderContext));
        // 判断商户类型
        MerchantAppChannelDO merchantAppChannelDO = channelProfitOrderContext.getMerchantAppChannelDO();
        // 是否普通商户
        boolean common = aliPayCommonParamsHandle.commonMer(merchantAppChannelDO);
        // 支付宝直付通配置信息
        CommonResult<AlipayConfig> alipayConfigCommonResult = null;
        // 普通商户
        if(common){
            channelProfitOrderContext.setRemark("支付宝直付通不支持普通商户");
            channelProfitOrderContext.setProcessStatus(false);
            return;
        }else{
            alipayConfigCommonResult = aliPayCommonParamsHandle.alipayConfig(channelProfitOrderContext.getProviderChannelConfig().getParamsConfig());
        }
        if(!alipayConfigCommonResult.isSuccess()){
            channelProfitOrderContext.setRemark(alipayConfigCommonResult.getMsg());
            channelProfitOrderContext.setProcessStatus(false);
            return;
        }
        PayOrderDO payOrderDO = channelProfitOrderContext.getPayOrderDO();
        try {

            List<RefundOrderFundDto> payOrderFundList = channelProfitOrderContext.getRefundOrderFundDtoList();
            // 退款批次号
            String refundBatchNo = payOrderFundList.get(0).getBatchNo();
            // 获取AlipayClient
            AlipayClient client = aliPayCommonParamsHandle.initClient(alipayConfigCommonResult.getData());
            // 请求参数
            AlipayTradeRefundModel model = new AlipayTradeRefundModel();
            model.setOutTradeNo(payOrderDO.getPayOrderNo());
            model.setRefundReason("分账退款");
            model.setOutRequestNo(refundBatchNo);

            // 组装分账退款详情
            List<OpenApiRoyaltyDetailInfoPojo> royaltyDetailInfoPojoList = new ArrayList<>();
            for(RefundOrderFundDto fundDto:payOrderFundList){
                OpenApiRoyaltyDetailInfoPojo royaltyDetailInfoPojo = new OpenApiRoyaltyDetailInfoPojo();
                royaltyDetailInfoPojo.setRoyaltyType("transfer");
                royaltyDetailInfoPojo.setTransOutType("loginName");
                royaltyDetailInfoPojo.setTransOut(fundDto.getAccountNo());
                royaltyDetailInfoPojo.setAmount(fundDto.getAmount());
                royaltyDetailInfoPojo.setDesc(fundDto.getDescription());
                royaltyDetailInfoPojoList.add(royaltyDetailInfoPojo);
            }
            model.setRefundRoyaltyParameters(royaltyDetailInfoPojoList);
            model.setRefundAmount("0");
            // 构造请求参数以调用接口
            AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
            request.setBizModel(model);

            log.info("支付宝直付通分账退款 请求参数:{}", JSONUtil.toJsonStr(model));
            AlipayTradeRefundResponse response = client.execute(request);
            log.info("支付宝直付通分账退款 响应参数:{}", JSONUtil.toJsonStr(response));

            if(response.isSuccess()){
                PayOrderEnums.PAY_ORDER_FUND_STATUS payOrderFundStatus = null;
                if(response.getFundChange().equals("Y")){
                    payOrderFundStatus = PayOrderEnums.PAY_ORDER_FUND_STATUS.REFUND;
                }else{
                    payOrderFundStatus = PayOrderEnums.PAY_ORDER_FUND_STATUS.REFUNDING;
                }
                for(RefundOrderFundDto payOrderFundDO:payOrderFundList){
                    payOrderFundDO.setFundStatus(payOrderFundStatus);
                }
            }else{
                channelProfitOrderContext.setProcessStatus(Boolean.FALSE);
                channelProfitOrderContext.setRemark(response.getSubMsg());
            }
        }catch (Exception e){
            log.error("支付宝直付通分账退款请求失败 message:{}", e);
            channelProfitOrderContext.setRemark("支付宝直付通分账退款请求失败,失败信息" + e.getMessage());
            channelProfitOrderContext.setProcessStatus(Boolean.FALSE);
        }
    }

    @Override
    public void queryRefundOrderProfit(ChannelProfitOrderQueryContext channelProfitOrderQueryContext) {
        log.info("支付宝直付通分账退款查询 请求参数:{}", JsonUtils.toJsonString(channelProfitOrderQueryContext));
        // 判断商户类型
        MerchantAppChannelDO merchantAppChannelDO = channelProfitOrderQueryContext.getMerchantAppChannelDO();
        // 是否普通商户
        boolean common = aliPayCommonParamsHandle.commonMer(merchantAppChannelDO);
        // 支付宝直付通配置信息
        CommonResult<AlipayConfig> alipayConfigCommonResult = null;
        // 普通商户
        if(common){
            channelProfitOrderQueryContext.setRemark("支付宝直付通不支持普通商户");
            channelProfitOrderQueryContext.setProcessStatus(false);
            return;
        }else{
            alipayConfigCommonResult = aliPayCommonParamsHandle.alipayConfig(channelProfitOrderQueryContext.getProviderChannelConfig().getParamsConfig());
        }
        if(!alipayConfigCommonResult.isSuccess()){
            channelProfitOrderQueryContext.setRemark(alipayConfigCommonResult.getMsg());
            channelProfitOrderQueryContext.setProcessStatus(false);
            return;
        }
        PayOrderDO payOrderDO = channelProfitOrderQueryContext.getPayOrderDO();
        try {

            List<OrderFundDetailDO> payOrderFundList = channelProfitOrderQueryContext.getRefundOrderFunds();
            // 批次号
            String batchNo = payOrderFundList.get(0).getBatchNo();

            // 获取AlipayClient
            AlipayClient client = aliPayCommonParamsHandle.initClient(alipayConfigCommonResult.getData());
            AlipayTradeFastpayRefundQueryRequest request = new AlipayTradeFastpayRefundQueryRequest();
            // 请求参数
            AlipayTradeFastpayRefundQueryModel model = new AlipayTradeFastpayRefundQueryModel ();
            model.setOutTradeNo(payOrderDO.getPayOrderNo());
            model.setOutRequestNo(batchNo);
            model.setQueryOptions(CollUtil.newArrayList("gmt_refund_pay","refund_detail_item_list"));
            request.setBizModel(model);

            log.info("支付宝直付通分账退款查询 请求参数:{}", JSONUtil.toJsonStr(model));
            AlipayTradeFastpayRefundQueryResponse response = client.execute(request);
            log.info("支付宝直付通分账退款查询 响应参数:{}", JSONUtil.toJsonStr(response));

            if(response.isSuccess()){
                PayOrderEnums.PAY_ORDER_FUND_STATUS payOrderFundStatus = PayOrderEnums.PAY_ORDER_FUND_STATUS.REFUND;
                for(OrderFundDetailDO payOrderFundDO:payOrderFundList){
                    payOrderFundDO.setFundStatus(payOrderFundStatus.getCode());
                }
            }else{
                channelProfitOrderQueryContext.setProcessStatus(Boolean.FALSE);
                channelProfitOrderQueryContext.setRemark(response.getSubMsg());
            }

        }catch (Exception e){
            log.error("支付宝直付通分账退款查询请求失败 message:{}",  e);
            channelProfitOrderQueryContext.setRemark("支付宝直付通分账退款查询请求失败,失败信息" + e.getMessage());
            channelProfitOrderQueryContext.setProcessStatus(Boolean.FALSE);
        }
    }

    @Override
    public void finishAcctOrder(ChannelProfitOrderContext channelProfitOrderContext) {
        OrderFundDO orderFundDO = channelProfitOrderContext.getOrderFundDO();
        orderFundDO.setFundStatus(PayOrderEnums.PAY_ORDER_FUND_STATUS.CLOSE.getCode());
    }

    @Override
    public CommonResult<String> acctStatusParse(HttpServletRequest request, ChannelProfitOrderContext channelProfitOrderContext) {

        CommonResult<String> acctOrderDOCommonResult = CommonResult.success();
        try {
            // 支付宝配置信息
            CommonResult<AlipayConfig> alipayConfigCommonResult = aliPayCommonParamsHandle.alipayConfig(channelProfitOrderContext.getProviderChannelConfig().getParamsConfig());
            if(!alipayConfigCommonResult.isSuccess()){
                log.info("支付宝直付通分账回调初始化配置失败,errMsg:{}", alipayConfigCommonResult.getMsg());
                acctOrderDOCommonResult.setCode(MerchantResultCode.MERCHANT_CHANNEL_CALLBACK_SIGN_ERROR.getCode());
                return acctOrderDOCommonResult;
            }
            AlipayConfig alipayConfig = alipayConfigCommonResult.getData();
            // 获取支付宝POST过来反馈信息
            Map<String, String> params = AliPayApi.toMap(request);
            log.info("支付宝直付通分账回调通知明文 {}", JSONUtil.toJsonStr(params));
            // boolean verifyResult = AlipaySignature.rsaCheckV1(params, alipayConfig.getPublicKey(), "UTF-8", "RSA2");
            // if(!verifyResult){
            //     log.info("支付宝直付通分账回调验签不通过");
            //     acctOrderDOCommonResult.setCode(MerchantResultCode.MERCHANT_CHANNEL_CALLBACK_SIGN_ERROR.getCode());
            //     return acctOrderDOCommonResult;
            // }

            AlipayZftDivideNotifyBizContentDto bizContentDto = JSONUtil.toBean(MapUtil.getStr(params,"biz_content"),AlipayZftDivideNotifyBizContentDto.class);
            // 分账详情
            List<OrderFundDetailDO> orderFundDetailList = channelProfitOrderContext.getOrderFundDetailDOList();
            List<RoyaltyDetail> royaltyDetails = bizContentDto.getRoyalty_detail_list();
            PayOrderEnums.PAY_ORDER_FUND_STATUS subPayOrderFundStatus = null;
            for(OrderFundDetailDO fundDetailDO:orderFundDetailList){
                Optional<RoyaltyDetail> aliProfitReceiverModelOptional = royaltyDetails.stream().filter(receiver -> receiver.getTransIn().equals(fundDetailDO.getAccountNo())).findFirst();
                if(aliProfitReceiverModelOptional.isPresent()){
                    RoyaltyDetail royaltyDetail = aliProfitReceiverModelOptional.get();
                    String subFailMsg = null;
                    // 通知类型
                    switch (royaltyDetail.getOperationType()){
                        // 分账通知
                        case "transfer":
                            switch (royaltyDetail.getState()){
                                case "PROCESSING":
                                    subPayOrderFundStatus = PayOrderEnums.PAY_ORDER_FUND_STATUS.FUNDING;
                                    break;
                                case "SUCCESS":
                                    subPayOrderFundStatus = PayOrderEnums.PAY_ORDER_FUND_STATUS.SUCCESS;
                                    break;
                                case "FAIL":
                                    subPayOrderFundStatus = PayOrderEnums.PAY_ORDER_FUND_STATUS.FAIL;
                                    subFailMsg = royaltyDetail.getErrorDesc();
                                    break;
                            }
                            fundDetailDO.setType(royaltyDetail.getTransInType());
                            break;
                        // 退分账通知
                        case "transfer_refund":
                            switch (royaltyDetail.getState()){
                                case "PROCESSING":
                                    subPayOrderFundStatus = PayOrderEnums.PAY_ORDER_FUND_STATUS.REFUNDING;
                                    break;
                                case "SUCCESS":
                                    subPayOrderFundStatus = PayOrderEnums.PAY_ORDER_FUND_STATUS.REFUND;
                                    break;
                                case "FAIL":
                                    subPayOrderFundStatus = PayOrderEnums.PAY_ORDER_FUND_STATUS.REFUND_FAIL;
                                    subFailMsg = royaltyDetail.getErrorDesc();
                                    break;
                            }
                            break;
                    }
                    fundDetailDO.setFundStatus(subPayOrderFundStatus.getCode());
                    fundDetailDO.setDetailId(royaltyDetail.getDetailId());
                    fundDetailDO.setSuccessTime(DateUtils.shanghaiDate(DateUtil.format(royaltyDetail.getExecuteDt(), DatePattern.NORM_DATETIME_PATTERN),  DatePattern.NORM_DATETIME_PATTERN));
                    if(StrUtil.isNotEmpty(subFailMsg)){
                        fundDetailDO.setFailReason(subFailMsg);
                    }
                }
            }
            channelProfitOrderContext.setPayOrderFundStatus(subPayOrderFundStatus);
        }
        catch (Exception e) {
            log.error("acctStatusParse error:{}",e);
            acctOrderDOCommonResult.setCode(MerchantResultCode.MERCHANT_CHANNEL_CALLBACK_SIGN_ERROR.getCode());
        }
        return acctOrderDOCommonResult;
    }

    @Override
    public void settleConfirm(ChannelProfitOrderContext channelProfitOrderContext) {
        // 判断商户类型
        MerchantAppChannelDO merchantAppChannelDO = channelProfitOrderContext.getMerchantAppChannelDO();
        // 是否普通商户
        boolean common = aliPayCommonParamsHandle.commonMer(merchantAppChannelDO);
        // 支付宝直付通配置信息
        CommonResult<AlipayConfig> alipayConfigCommonResult = null;
        // 普通商户
        if(common){
            channelProfitOrderContext.setRemark("支付宝直付通不支持普通商户");
            channelProfitOrderContext.setProcessStatus(false);
            return;
        }else{
            alipayConfigCommonResult = aliPayCommonParamsHandle.alipayConfig(channelProfitOrderContext.getProviderChannelConfig().getParamsConfig());
        }
        if(!alipayConfigCommonResult.isSuccess()){
            channelProfitOrderContext.setRemark(alipayConfigCommonResult.getMsg());
            channelProfitOrderContext.setProcessStatus(false);
            return;
        }
        try {
            PayOrderDO payOrderDO = channelProfitOrderContext.getPayOrderDO();
            // 服务商
            AliPayZftChannelParams aliPayChannelParams = JsonUtils.parseObject(merchantAppChannelDO.getAppConfigJson(), AliPayZftChannelParams.class);
            // 获取AlipayClient
            AlipayClient client = aliPayCommonParamsHandle.initClient(alipayConfigCommonResult.getData());
            // 异步结算
            commonTasks.alipayZftSettleConfirm(client,payOrderDO,aliPayChannelParams.getSettleNo(),alipayConfigCommonResult.getData());

        }catch (Exception e) {
            log.error("支付宝直付通订单资金结算请求失败 message:{}", e.getMessage(), e);
            channelProfitOrderContext.setRemark("支付宝直付通订单资金结算请求失败,失败信息" + e.getMessage());
            channelProfitOrderContext.setProcessStatus(Boolean.FALSE);
        }

    }
}
